package restaurant.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import restaurant.gui.Square.Type;

/**
 * Implement a JPanel to represent the environment and the agent
 *
 */
public class GridPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel[][] labelMatrix;
	public static ImageIcon tableEmptyIcon, tableFullIcon, customerIcon, waiterExpertIcon, waiterIcon, tileIcon, doorIcon, kitchenIcon;
	public int labelSize;
	static Environment env;
	
	static Square.Type currType;
	static ImageIcon currIcon;
	
	/**
	 * Generate a graphic representation of the environment and its elements
	 * @param env an environment configuration
	 */
	public GridPanel(MainJFrame mainJFrame, Environment env){
		GridPanel.env = env;
		init();
		update();
	}
	
	/**
	 * Generate a GridLayout according with the width and length of the floor
	 * and set the images that correspond to each element of the environment
	 */
	void init() {
		
		setLayout(new FlowLayout());
		
		JPanel flowPanel = new JPanel();
		add(flowPanel);
		flowPanel.setLayout(new GridLayout(env.floor.length, env.floor.width));
		
		tableEmptyIcon = ImageLoader.tableEmptyIcon;
		tableFullIcon = ImageLoader.tableFullIcon;
		customerIcon = ImageLoader.customerIcon;
		waiterExpertIcon = ImageLoader.waiterExpertIcon;
		waiterIcon = ImageLoader.waiterIcon;
		tileIcon = ImageLoader.tileIcon;
		doorIcon = ImageLoader.doorIcon;
		kitchenIcon = ImageLoader.kitchenIcon;
		 
		
		labelMatrix = new JLabel[env.floor.length][env.floor.width];
		
		currType = Type.TABLE_EMPTY;
		currIcon = tableEmptyIcon;
		
		for(int i=0; i<env.floor.length; i++)
			for(int j=0; j<env.floor.width; j++){
				GridBagConstraints constraints = new GridBagConstraints();
				constraints.fill = GridBagConstraints.BOTH;
				constraints.gridx = i;
				constraints.gridy = j;
				final JLabel label = new JLabel();
				label.setPreferredSize(new Dimension(ImageLoader.iconSize,ImageLoader.iconSize));
//				label.addMouseListener(new ClickHandler(label,i,j));
				labelMatrix[i][j] = label;
				flowPanel.add(label, constraints);
			}
	}
	/**
	 * Generate a graphic representation of the environment 
	 * on a thread, according with the state of each tile (square)
	 * and the position of the agent
	 */
	public void update() {
		labelMatrix[0][0].setIcon(doorIcon);
		for(int i=0; i<env.floor.length; i++)
			for(int j=0; j<env.floor.width; j++){
				if(env.floor.get(i,j) == Square.Type.TABLE_EMPTY)
					labelMatrix[i][j].setIcon(tableEmptyIcon);
				if(env.floor.get(i,j) == Square.Type.TABLE_FULL)
					labelMatrix[i][j].setIcon(tableFullIcon);
				if(env.floor.get(i,j) == Square.Type.CLEAN)
					labelMatrix[i][j].setIcon(tileIcon);
//				if(env.floor.get(i,j) == Square.Type.WAITER)
//					labelMatrix[i][j].setIcon(waiterIcon);
//				if(env.floor.get(i,j) == Square.Type.WAITER_EXPERT)
//					labelMatrix[i][j].setIcon(waiterExpertIcon);
				if(env.floor.get(i,j) == Square.Type.CUSTOMER)
					labelMatrix[i][j].setIcon(customerIcon);
				if(env.floor.get(i,j) == Square.Type.DOOR)
					labelMatrix[i][j].setIcon(doorIcon);
				if(env.floor.get(i,j) == Square.Type.KITCHEN)
					labelMatrix[i][j].setIcon(kitchenIcon);
				
//				for (Agent customer : env.customersAgent) {
//					if(customer.x == i && customer.y == j)
//						labelMatrix[i][j].setIcon(customerIcon);
//				}
//				for (Agent waiter : env.waitersAgent) {
//					if(waiter.x == i && waiter.y == j)
//						labelMatrix[i][j].setIcon(waiterExpertIcon);
//				}
				for(Agent agent : env.agents)
					if(agent.x == i && agent.y == j && agent.type == Square.Type.CUSTOMER)
						labelMatrix[i][j].setIcon(customerIcon);
					else if(agent.x == i && agent.y == j && agent.type == Square.Type.WAITER)
						labelMatrix[i][j].setIcon(waiterIcon);
				labelMatrix[0][0].setIcon(doorIcon);
				
			}
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}